#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- 
# --
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2021/4/12 12:14
# Email lmay@lmaye.com
# ----------------------------------------------------------
from constant.sys_enum import SysEnum
from core import LOG
from utils import jdbc_util, excel_utils
from utils.read_config import read_yml


def data_handler():
    db_yml = read_yml(SysEnum.DB_PATH.value)
    connect = jdbc_util.sql_connect(db_yml["db"])
    # 读取excel文件
    path = SysEnum.RESOURCES_PATH.value + SysEnum.SEPARATOR.value
    # 根据提供的Excel导数据
    records = excel_utils.read_excel(path + "2020.xlsx", 0)
    row = 0
    for it in records:
        row += 1
        data = "'" + str(it["订单编号"]) + "'"
        sql = "select id, site_id, site_trade_id from ecs_erp_orders  where site_trade_id = %s" % data
        record = jdbc_util.sql_execute(sql, "select", connect=connect)
        if record:
            site_id_ = record[0]["site_id"]
            if 3 == site_id_:
                # excel_utils.write_excel_append(path + "2020.xls", row, "美国亚马逊订单，胜途有")
                it["Flag"] = "美国亚马逊订单，胜途有"
            else:
                # excel_utils.write_excel_append(path + "2020.xls", row, str(site_id_))
                it["Flag"] = str(site_id_)
        else:
            # excel_utils.write_excel_append(path + "2020.xls", row, "")
            it["Flag"] = ""
        LOG.info("读取当前行: {}".format(row))
    excel_utils.write_excel(path + "tmp.xls", "Sheet1", records)
